﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LotteryVote.Data;
using LotteryVote.Entity;
using LotteryVote.Utility;

namespace LotteryVote.Logic
{
    public class CompanyLotteryCycleFacade
    {
        private CompanyLotteryCycleDataAccess _daCompanyCycle;
        internal CompanyLotteryCycleDataAccess DaCompanyCycle
        {
            get
            {
                if (_daCompanyCycle == null)
                    _daCompanyCycle = new CompanyLotteryCycleDataAccess();
                return _daCompanyCycle;
            }
            set
            {
                _daCompanyCycle = value;
            }
        }

        public void AddCycle(int companyId, DayOfWeek day)
        {
            if (IsExistCycle(companyId, day))
                return;
            CompanyLotteryCycle cycle = new CompanyLotteryCycle
            {
                CompanyId = companyId,
                DayOfWeek = day
            };
            DaCompanyCycle.Insert(cycle);
        }
        public void UpdateCycles(int companyId, IList<DayOfWeek> days)
        {
            DaCompanyCycle.ExecuteWithTransaction(() =>
            {
                DaCompanyCycle.Clear(companyId);
                days.ForEach(it =>
                {
                    DaCompanyCycle.Insert(new CompanyLotteryCycle { CompanyId = companyId, DayOfWeek = it });
                });
            });
        }

        public IList<CompanyLotteryCycle> GetCycles(int companyId)
        {
            return DaCompanyCycle.GetCycles(companyId);
        }
        public bool IsExistCycle(int companyId, DayOfWeek day)
        {
            return DaCompanyCycle.GetCycle(companyId, day) != null;
        }
    }
}
